<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateUserTable extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('think_user');
    $table
        ->addColumn('nickname', 'string', ['limit' => 50, 'default' => '', 'comment' => '用户昵称'])
        ->addColumn('mobile', 'string', ['limit' => 20, 'default' => '', 'comment' => '用户手机号'])
        ->addColumn('openid', 'string', ['limit' => 64, 'default' => '', 'comment' => '微信openid'])
        ->addColumn('unionid', 'string', ['limit' => 64, 'default' => '', 'comment' => '微信unionid'])
        ->addColumn('avatar', 'string', ['limit' => 255, 'default' => '', 'comment' => '用户头像'])
        ->addColumn('is_vip', 'boolean', ['default' => 0, 'comment' => '是否是VIP'])
        ->addColumn('vip_expire_at', 'datetime', ['null' => true, 'comment' => 'VIP到期日期'])
        ->addColumn('balance', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '用户余额'])
        ->addColumn('created_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建日期'])
        ->addColumn('last_login_at', 'datetime', ['null' => true, 'comment' => '登录日期'])
        ->addColumn('token', 'string', ['null' => true, 'comment' => '登录token'])
        ->addIndex(['openid'], ['unique' => true, 'name' => 'idx_openid_unique'])
        ->create();
    }
}
